package com.example.blogapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.blogapi.domain.MsComment;
import com.example.blogapi.domain.MsSysUser;
import com.example.blogapi.mapper.MsCommentMapper;
import com.example.blogapi.service.CommentsService;
import com.example.blogapi.service.MsSysUserService;
import com.example.blogapi.utils.UserThreadLocal;
import com.example.blogapi.vo.CommentVo;
import com.example.blogapi.vo.Result;
import com.example.blogapi.vo.UserVo;
import com.example.blogapi.vo.param.CommentParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentsServiceImpl implements CommentsService {
    @Autowired
    MsCommentMapper commentMapper;
    @Autowired
    MsSysUserService userService;

    public CommentVo copy(MsComment comment){
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);
        //时间格式化
//        commentVo.setCreateDate(Long.valueOf(new Date().toString()));
        Long authorId = comment.getAuthorId();
        UserVo userVo = userService.findUserVoById(authorId);
        commentVo.setAuthor(userVo);
        if (comment.getLevel() == 1){
            //评论的评论
            List<CommentVo> commentVoList = findCommentsByParentId(comment.getId());
            commentVo.setChildren(commentVoList);
        }
        if (comment.getLevel() > 1) {
            Long toUid = comment.getToUid();
            UserVo toUserVo = userService.findUserVoById(toUid);
            commentVo.setToUser(toUserVo);
        }
        return commentVo;
    }

    private List<CommentVo> findCommentsByParentId(Long id) {
        LambdaQueryWrapper<MsComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MsComment::getParentId,id);
        queryWrapper.eq(MsComment::getLevel,2);
        List<MsComment> comments = this.commentMapper.selectList(queryWrapper);
        return copyList(comments);
    }

    public List<CommentVo> copyList(List<MsComment> commentList){
        List<CommentVo> commentVoList = new ArrayList<>();
        for (MsComment comment : commentList) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }
    @Override
    public Result commentsByArticleId(Long articleId) {
        LambdaQueryWrapper<MsComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MsComment::getArticleId,articleId);
        queryWrapper.eq(MsComment::getLevel,1);
        List<MsComment> comments = commentMapper.selectList(queryWrapper);

        return Result.success(copyList(comments));
    }

    /**
     * 发送评论
     * @param commentParam
     * @return
     */
    @Override
    public Result sendComment(CommentParam commentParam) {
        MsSysUser sysUser = UserThreadLocal.get();
        MsComment comment = new MsComment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(sysUser.getId());
        comment.setContent(commentParam.getContent());
        comment.setCreateDate(System.currentTimeMillis());
        Long parent = commentParam.getParent();
        if (parent == null || parent == 0) {
            comment.setLevel(1);
        }else{
            comment.setLevel(2);
        }
        //如果是一级评论则把父亲id设置为0
        comment.setParentId(parent == null ? 0 : parent);
        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        this.commentMapper.insert(comment);
        return Result.success(null);
    }
}
